//fireworks 圆顶点
function firework_vertices_generate(num) {
	let firework_vertices = [];

	for (let i = 0; i < num; i += 1) {

		let angle = i / num * Math.PI * 2.0;
		//顶点坐标
		firework_vertices.push(Math.cos(angle), Math.sin(angle));

	}

	return firework_vertices;
}
function firework_indices_generate(num) {
	let firework_indices = [];

	for (let i = 0; i < num - 2; i += 1) {

		firework_indices.push(0, i + 1, i + 2);	
	}

	return firework_indices;
}
function firework_sphere_generate(num_span) {
	const pi = 3.1415926;
	let coord_xyz = [];
	for (let i = 1; i < num_span; i += 1) {
		for (let j = 0; j < num_span * 2; j += 1) {

			coord_xyz.push(Math.sin(pi / num_span * i) * Math.cos(pi / num_span * j));
			coord_xyz.push(Math.sin(pi / num_span * i) * Math.sin(pi / num_span * j));
			coord_xyz.push(Math.cos(pi / num_span * i));
		}
	}

	return coord_xyz;
}


function VAO_firework_generate(gl, programInfo_firework, num_vertices, num_firework) {
	//const num = 10;
	const VAO_firework = gl.createVertexArray();
	gl.bindVertexArray(VAO_firework);
	const VBO_firework = gl.createBuffer();
	gl.bindBuffer(gl.ARRAY_BUFFER, VBO_firework);
	{
		const firework = firework_vertices_generate(num_vertices);//firework顶点数据
		gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(firework), gl.STATIC_DRAW);

		gl.vertexAttribPointer(programInfo_firework.attribLocations.vertexPosition, 2, gl.FLOAT, false, 8, 0);
		gl.enableVertexAttribArray(programInfo_firework.attribLocations.vertexPosition);
	}
	const EBO_firework = gl.createBuffer();
	gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, EBO_firework);
	{
		const firework_indices = firework_indices_generate(num_vertices);
		gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(firework_indices), gl.STATIC_DRAW);
	}
	//实例化数据
	const instanceVBO_firework = gl.createBuffer();
	gl.bindBuffer(gl.ARRAY_BUFFER, instanceVBO_firework);
	{
		const instancefirework = firework_sphere_generate(num_firework);//firework顶点数据
		gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(instancefirework), gl.STATIC_DRAW);

		gl.vertexAttribPointer(programInfo_firework.attribLocations.instanceDirection, 3, gl.FLOAT, false, 12, 0);
		gl.enableVertexAttribArray(programInfo_firework.attribLocations.instanceDirection);

		gl.vertexAttribDivisor(programInfo_firework.attribLocations.instanceDirection, 1);
	}

	return VAO_firework;
}